package com.yunfeisoft.business.service.impl;

import com.applet.base.BaseServiceImpl;
import com.yunfeisoft.business.dao.inter.SmsHomeAdvertiseDao;
import com.yunfeisoft.business.model.SmsHomeAdvertise;
import com.yunfeisoft.business.service.inter.SmsHomeAdvertiseService;
import com.applet.sql.separation.DataSourceChange;
import com.applet.utils.Page;
import com.yunfeisoft.dao.inter.AttachmentDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * ClassName: SmsHomeAdvertiseServiceImpl
 * Description: 首页轮播广告表service实现
 * Author: Jackie liu
 * Date: 2020-06-02
 */
@Service("smsHomeAdvertiseService")
public class SmsHomeAdvertiseServiceImpl extends BaseServiceImpl<SmsHomeAdvertise, String, SmsHomeAdvertiseDao> implements SmsHomeAdvertiseService {

    @Autowired
    private AttachmentDao attachmentDao;

    @Override
    @DataSourceChange(slave = true)
    public Page<SmsHomeAdvertise> queryPage(Map<String, Object> params) {
        return getDao().queryPage(params);
    }

    @Transactional
    @Override
    public void saveAdvertise(SmsHomeAdvertise advertise, String attachmentId) {
        advertise.setClickCount(0);
        advertise.setOrderCount(0);
        if (advertise.getSort() == null) {
            advertise.setSort(0);
        }
        // 保存广告
        getDao().insert(advertise);
        // 保存图片关联
        attachmentDao.updateRefId(new String[]{attachmentId}, advertise.getId());
    }

    @Transactional
    @Override
    public void modifyAdvertise(SmsHomeAdvertise advertise, String attachmentId) {
        // 修改广告
        getDao().update(advertise);
        if (StringUtils.isNotBlank(attachmentId)) {
            // 保存图片关联
            attachmentDao.updateRefId(new String[]{attachmentId}, advertise.getId());
        }
    }

    /**
     * 查询广告列表
     * @param params
     * @return
     */
    @Override
    public List<SmsHomeAdvertise> queryAdvertList(Map<String, Object> params) {
        return getDao().queryAdvertList(params);
    }
}